/*
 * Copyright 2009 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.gdata.data.contacts;

import nl.biz_e.gdata.data.AbstractBaseManager;
import nl.biz_e.gdata.util.GDataAuthentication;
import nl.biz_e.gdata.util.Utils;

import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.util.AuthenticationException;

/**
 * @author Joost A. Bloemsma
 * 
 */
public class ContactsManager extends
        AbstractBaseManager<ContactEntry, ContactFeed> {

    /**
     * The projection used by the returning feeds.<br/>
     * See <a href="http://code.google.com/intl/nl/apis/contacts/docs/2.0/reference.html#Projections"
     * >Google reference</a>
     */
    public enum Projection {
        /** No gd:extendedProperty elements are returned/updated */
        THIN,
        /**
         * All gd:extendedProperty elements are returned and all of them have to
         * be included during an update.
         */
        FULL,
        /**
         * A custom projection, specified using
         * {@link ContactsManager#setCustomProjection(String)}
         */
        CUSTOM
    }

    private static final String FEEDNAME = "contacts";
    private final ContactsService service;
    private String customProjection = "";
    private Projection projection = Projection.FULL;

    /**
     * Constructs a new ContactsManager instance
     * 
     * @param authentication
     *            Used for authentication agains the GData Service
     * @param appName
     *            The application name
     * @throws AuthenticationException
     *             If authentication fails
     */
    public ContactsManager(GDataAuthentication authentication, String appName)
            throws AuthenticationException {
        super(authentication, FEEDNAME, ContactFeed.class, ContactEntry.class,
                appName);
        this.service = new ContactsService(this.getAppName());
        this.init();
    }

    /**
     * Constructs a new ContactsManager instance.<br/>
     * The application name must have been set to the static
     * {@link Utils#APPNAME} variable.
     * 
     * @param authentication
     *            Used for authentication agains the GData Service
     * @throws AuthenticationException
     *             If authentication fails
     */
    public ContactsManager(GDataAuthentication authentication)
            throws AuthenticationException {
        this(authentication, Utils.APPNAME);
    }

    /**
     * @return the customProjection
     */
    public String getCustomProjection() {
        return customProjection;
    }

    /**
     * Sets the customProjection
     * 
     * @param customProjection
     *            the customProjection to set
     */
    public void setCustomProjection(String customProjection) {
        this.customProjection = customProjection;
    }

    /**
     * @param projection
     *            the projection to set
     */
    public void setProjection(Projection projection) {
        this.projection = projection;
    }

    /**
     * @return the projection
     */
    public Projection getProjection() {
        return projection;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getUrlExtension() {
        switch (this.projection) {
            case FULL:
                return "full/";
            case THIN:
                return "thin/";
            case CUSTOM:
                return customProjection.length() > 0 ? customProjection + "/"
                        : "";
            default:
                this.setProjection(Projection.THIN);
                return getUrlExtension();
        }
    }

    private void init() throws AuthenticationException {
        this.setService(service);
    }
}
